

Berta.EditorManager.addPlugin('OnOff', new Class(function() {
	
	return {
		extend: Berta.Editor,
		pluginName: 'Textline',
		
		// defaults for this particular plugin
		defaults: {
			pluginOptions: {
				on: 'on',
				off: 'off'
				
				// see defaults.pluginOptions in Berta.Editor class for inherited options
			}
		},
		
		constructor: function(options) {
			// call the parent with pluginName=null
			// Editor defaults get extended by this.defaults and then extended by options
			Berta.Editor.call(this, options);
			
			this.originalValue = this._(properValue)(this.element.html());
			this.value = this.originalValue;
			
		},
		
		
		//   .______    __    __  .______    __       __    ______    ////////////////////////////////////////////////////////////
		//   |   _  \  |  |  |  | |   _  \  |  |     |  |  /      |   ////////////////////////////////////////////////////////////
		//   |  |_)  | |  |  |  | |  |_)  | |  |     |  | |  ,----'   ////////////////////////////////////////////////////////////
		//   |   ___/  |  |  |  | |   _  <  |  |     |  | |  |        ////////////////////////////////////////////////////////////
		//   |  |      |  `--'  | |  |_)  | |  `----.|  | |  `----.   ////////////////////////////////////////////////////////////
		//   | _|       \______/  |______/  |_______||__|  \______|   ////////////////////////////////////////////////////////////
		
		on: /* API */ function() {
			this.element.addClass('x-editable-on x-editable-onoff');
			this.element.bind('click' + this.namespace(), this._(onElementClick));
			
			this._(updateElement)(this.value);
			
			Berta.Editor.prototype.on.call(this);
		},
		
		off: /* API */ function() {
			if(this.hasFlag(Berta.Editor.flags.EDITING)) {
				this.cancel();
			}
			this.element.removeClass('x-editable-on x-editable-onoff');
			this.element.unbind('click' + this.namespace());
			
			// put in the element the non-editable version of the value
			this.element.text(String(this.value));
			
			Berta.Editor.prototype.off.call(this);
		},
		
		edit: /* API */ function() {
			if(this.hasFlag(Berta.Editor.flags.ON) 
				&& !this.hasFlag(Berta.Editor.flags.EDITING) 
				&& !this.hasFlag(Berta.Editor.flags.SAVING)) {
				
				Berta.Editor.prototype.startEditing.call(this);
				
				// invert the value for saving
				this.valueToSave = !this.value;
				
				this.save();
				
				// this type cannot have context variables
				//Berta.Editor.prototype.setPanelContext.call(this);
			}
		},
		
		save: /* API */ function() {
			if(this.hasFlag(Berta.Editor.flags.EDITING)) {
				this.value = this.valueToSave;
				this._(updateElement)(this.value);
				if(this.value != this.originalValue) this.fireChange();
				
				Berta.Editor.prototype.stopEditing.call(this);
				Berta.Editor.prototype.save.call(this, this._(onSaveComplete));
			}
		},
		
		cancel: /* API */ function() {
			//Berta.Editor.prototype.hidePanelContext.call(this);
			if(this.hasFlag(Berta.Editor.flags.SAVING)) {
				this.saveStop();
			}
			
			this.value = this.originalValue;
			this._(updateElement)(this.value);
			this.fireRevert();
			this.cleanup();
		},

		
		cleanup: function() {
			if(this.hasFlag(Berta.Editor.flags.DESTROYING)) {
				this.off();
			}
		},
		
		
		validate: /* override */ function() {
			Berta.Editor.prototype.validate.call(this);
		}

		
	};
	
	
	
	
	//   .______   .______     __  ____    ____  ___   .___________. _______    //////////////////////////////////////////////////
	//   |   _  \  |   _  \   |  | \   \  /   / /   \  |           ||   ____|   //////////////////////////////////////////////////
	//   |  |_)  | |  |_)  |  |  |  \   \/   / /  ^  \ `---|  |----`|  |__      //////////////////////////////////////////////////
	//   |   ___/  |      /   |  |   \      / /  /_\  \    |  |     |   __|     //////////////////////////////////////////////////
	//   |  |      |  |\  \--.|  |    \    / /  _____  \   |  |     |  |____    //////////////////////////////////////////////////
	//   | _|      | _| `.___||__|     \__/ /__/     \__\  |__|     |_______|   //////////////////////////////////////////////////
	
	
	function properValue(val) {
		var v = String(val).trim();
		return val === this.options.pluginOptions.on || val === '1' || val === 'true';
	}
	
	function updateElement(isOn) {
		this.element.html(isOn ? this.options.pluginOptions.on : this.options.pluginOptions.off);
	}
	
	
	
	function onElementClick() {
		this.edit();
		//return false;
	}
	
	function onSaveComplete(response) {
		if (response.error) {
			this.askIfCancelOnSaveErorr(response.errorMessage, this._(this.cancel), this._(this.edit));
		} else {
			this.value = response.json.values[this.options.variable];
			this._(updateElement)(this.value);
			
			this.fireSave();
			
			// update original value after calling the callback, so the callback can distinguish between the two
			this.originalValue = this.value;
			
			this.cleanup();
		}
	}
	

}()));



